var Messages = require('../models/messages');
var express = require('express');
var router = express.Router();

router.route('/messages')

    // create a bear (accessed at POST http://localhost:8080/api/bears)
    .post(function(req, res) {
        
        var mg = new Messages();      // create a new instance of the Bear model
        mg.author = req.body.author; 
        mg.message = req.body.message;
        mg.date = req.body.date
       // mg.date = new Date();
        // save the bear and check for errors
        mg.save(function(err) {
            if (err)
                res.json({message: 'failed'});
            res.json({ message: 'message created!' });
        });   
    })

    .get(function(req, res) {
      Messages.find(function(err, message) {
        if (err) {
         return res.send(err);
        }
        res.json(message);
      })
    });

module.exports = router;